7. Miscellaneous



This section describes some additional useful data types, functions and macros of LEDA. They can be used in any program that includes the <LEDA/basic.h> header file.



7.1 Streams

The stream data types described in this section are all derived from the stream types istream and ostream. Some of these types may be obsolete in combination with the latest versions of the standard I/O library.



7.1.1 File input streams (file_istream)

1. Definition

An instance I of the data type file$\_istream$ is an istream connected to a file F, i.e., all input operations or operators applied to I read from F.

2. Creation

file$\_istream$   I(string s);

creates an instance I of type file_istream connected to the file with name s.

3. Operations

All operations and operators (> >) defined for istreams can be applied to file input streams as well.



7.1.2 File output streams (file_ostream)

1. Definition

An instance O of the data type file$\_ostream$ is an ostream connected to a file F, i.e., all output operations or operators applied to O write to F.

2. Creation

file$\_ostream$   O(string s);

creates an instance O of type file_ostream connected to the file with name s.

3. Operations

All operations and operators (< <) defined for ostreams can be applied to file output streams as well.


7.1.3 String input streams (string_istream)

1. Definition

An instance I of the data type string$\_istream$ is an istream connected to a string s, i.e., all input operations or operators applied to I read from s.

2. Creation

string$\_istream$   I(string s);

creates an instance I of type string_istream connected to the string s.

3. Operations

All operations and operators (> >) defined for istreams can be applied to string input streams as well.


7.1.4 String output streams (string_ostream)

1. Definition

An instance O of the data type string$\_ostream$ is an ostream connected to an internal string buffer, i.e., all output operations or operators applied to O write into this internal buffer. The current value of the buffer is called the contents of O.

2. Creation

string$\_ostream$   O;

creates an instance O of type string_ostream.

3. Operations


truecm &truecm &


string &O.clear() &clears the contents of O


string &O.str() &returns the current contents of O

All operations and operators (< <) defined for ostreams can be applied to string output streams as well.


7.1.5 Command input streams (cmd_istream)

1. Definition

An instance I of the data type cmd$\_istream$ is an istream connected to the output of a shell command cmd, i.e., all input operations or operators applied to I read from the standard output of command cmd.

2. Creation

cmd$\_istream$   I(string cmd );

creates an instance I of type cmd_istream connected to the output of command cmd.

3. Operations

All operations and operators (> >) defined for istreams can be applied to command input streams as well.


7.1.6 Command output streams (cmd_ostream)

1. Definition

An instance O of the data type cmd$\_ostream$ is an ostream connected to the input of a shell command cmd, i.e., all output operations or operators applied to O write into the standard input of command cmd.

2. Creation

cmd$\_ostream$   O(string cmd );

creates an instance O of type cmd_ostream connected to the input of command cmd.

3. Operations

All operations and operators (< <) defined for ostreams can be applied to command output streams as well.



7.2 Some useful functions and macros

truecm &truecm &


int &read_int(string s = ``'') &prints s and reads an integer

char &read_char(string s = ``'') &prints s and reads a character

double &read_real(string s = ``'') &prints s and reads a real number

string &read_string(string s = ``'') &prints s and reads a line of input

bool &Yes(string s = ``'') &returns (read_char(s) == `y')


void &init_random() &initializes the random number generator.

double &random() &returns a real valued random number in [0, 1]

int &random(int a, int b) &returns a random integer in [a..b]


float &used_time() &returns the currently used cpu time in seconds.

float &used_time(float& T) &returns the cpu time used by the program from & &T up to this moment and assings the current & &time to T.

void &print_statistics() &prints a summary of the currently used memory


newline truecm &cout < < ``" forever &for(;;) loop(a,b,c) &for ( a = b;a < = c;a + +) Max(a,b) &( (a > b) ? a  :  b) Min(a,b) &( (a > b) ? b  :  a)



7.3 Memory Management

LEDA offers an efficient memory management system that is used internally for all node, edge and item types. This system can easily be customized for user defined classes by the ``LEDA_MEMORY" macro. You simply have to add the macro call ``LEDA_MEMORY(T)" to the declaration of a class T. This creates new and delete operators for type T allocating and deallocating memory using LEDA's internal memory manager. We continue the example from section 1.5:


struct pair {     & double  x; & double  y; & pair() { x = y = 0; } &pair(const pairp) { x = p.xy = p.y; } &friend ostream& &operator(ostream&,const pair&) { ...} &friend istream& &operator(istream&,pair&) { ...} &friend int &compare(const pairp, const pairq) { ...} &LEDA_MEMORY(pair) };

dictionarypair,int D;



7.4 Error Handling

LEDA tests the preconditions of many (not all!) operations. Preconditions are never tested, if the test takes more than constant time. If the test of a precondition fails an error handling routine is called. It takes an integer error number i and a char* error message string s as arguments. It writes s to the diagnostic output (cerr) and terminates the program abnormally if i≠ 0. Users can provide their own error handling function handler by calling

set_error_handler(handler).

After this function call handler is used instead of the default error handler. handler must be a function of type void  handler(int, char*). The parameters are replaced by the error number and the error message respectively.

10cm